﻿#include "master.h"

Master::Master() : QObject()
{
	wait_time = 1000;
}

void Master::sendRequest(QString portName, QString str)
{
	if (m_serial.portName() != portName) {
		m_serial.close();
		m_serial.setPortName(portName);

		if (!m_serial.open(QIODevice::ReadWrite)) {
			processError(tr("Can't open %1, error code %2")
				.arg(m_serial.portName()).arg(m_serial.error()));
			return;
		}
	}

	setControlsEnabled(false);
	//	m_statusLabel->setText(tr("Status: Running, connected to port %1.").arg(portName));

	m_serial.write(str.toUtf8());
	m_timer.start(wait_time);
}

void Master::readResponse()
{
	m_response.append(m_serial.readAll());
}

void Master::processTimeout()
{
	setControlsEnabled(true);
	//  m_trafficLabel->setText(tr("Traffic, transaction #%1:"
	//                             "\n\r-request: %2"
	//                             "\n\r-response: %3")
	//                              .arg(++m_transactionCount)
	//                              .arg(m_requestLineEdit->text())
	//                              .arg(QString::fromUtf8(m_response)));
	m_response.clear();
}

void Master::processError(const QString& error)
{
	setControlsEnabled(true);
	//m_statusLabel->setText(tr("Status: Not running, %1.").arg(error));
//	m_trafficLabel->setText(tr("No traffic."));
}
